package com.suning.sawp.service.impl.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.intf.dao.StoreManSubProductDaoService;
import com.suning.sawp.po.StoreManSubProduct;

@Service("storeManSubProductDaoService")
public class StoreManSubProductDaoServiceImpl implements StoreManSubProductDaoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(StoreManSubProductDaoServiceImpl.class);
    
    @Resource
    DalClient dalClient;
    
    @Override
    public boolean subscribeProduct(String staffId, String productId, String productName) {
        boolean flag = false;
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("staffId", staffId);
            paramMap.put("productId", productId);
            paramMap.put("productName", productName);
            int ret = dalClient.execute("PRDOUCT_PRICE.SUBSCRIBE_PRODUCT", paramMap);
            flag = ret > 0 ? true : false;
        } catch (Exception e) {
            StringBuilder params = new StringBuilder();
            params.append("staffId:").append(staffId).append(", productId:").append(productId)
                .append(", productName:").append(productName);
            LOGGER.error("店员关注商品入库失败，params:{}", params.toString(), e);
        }
        return flag;
    }

    @Override
    public boolean unSubscribeProduct(String staffId, String productId) {
        boolean flag = false;
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("staffId", staffId);
            paramMap.put("productId", productId);
            int ret = dalClient.execute("PRDOUCT_PRICE.UN_SUBSCRIBE_PRODUCT", paramMap);
            flag = ret > 0 ? true : false;
        } catch (Exception e) {
            StringBuilder params = new StringBuilder();
            params.append("staffId:").append(staffId).append(", productId:").append(productId);
            LOGGER.error("店员取消商品关注失败，params:{}", params.toString(), e);
        }
        return flag;
    }

    @Override
    public int countMySubProducts(String staffId, String productId) {
        int total = 0;
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("staffId", staffId);
            if (StringUtils.isNotBlank(productId)) {
                paramMap.put("productId", productId);
            }
            total = dalClient.queryForObject("PRDOUCT_PRICE.COUNT_STORE_MAN_PDCTS",
                    paramMap, Integer.class);
        } catch (Exception e) {
            LOGGER.error("查询店员关注商品总数失败， staffId:{}", staffId, e);
        }
        return total;
    }

    @Override
    public List<StoreManSubProduct> getSubProductList(String staffId) {
        List<StoreManSubProduct> list = null;
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("staffId", staffId);
            list = dalClient.queryForList("PRDOUCT_PRICE.MY_SUB_PRODUCTS", 
                    paramMap, StoreManSubProduct.class);
        } catch (Exception e) {
            LOGGER.error("查询店员关注的商品列表失败, staffId:{}", staffId, e);
        }
        return list;
    }

}
